//package org.ricks.robot;
//
//import org.ricks.net.transport.kcp.KcpSession;
//import proto.common.FProto;
//
//import java.util.HashMap;
//import java.util.HashSet;
//import java.util.Map;
//import java.util.Set;
//import java.util.concurrent.ConcurrentHashMap;
//
//public class Monitor {
//
//    public static Map<KcpSession, Map<FProto.Action, ActionMonitorInfo>>  actionReqCount = new ConcurrentHashMap<>();
//
//    public static synchronized void addActionRequest(KcpSession session,  FProto.Action action) {
//        Map<FProto.Action, ActionMonitorInfo> actionMonitorInfoMap = actionReqCount.computeIfAbsent(session, (k) -> new HashMap<>());
//        ActionMonitorInfo actionMonitorInfo = actionMonitorInfoMap.computeIfAbsent(action, (k) -> new ActionMonitorInfo(session, action, 0));
//        actionMonitorInfo.requestCount++;
//    }
//
//    public static synchronized void addActionSuccess(KcpSession session, FProto.Action action , long costTime) {
//        Map<FProto.Action, ActionMonitorInfo> actionMonitorInfoMap = actionReqCount.get(session);
//            ActionMonitorInfo actionMonitorInfo = actionMonitorInfoMap.get(action);
//            if(actionMonitorInfo == null && action == FProto.Action.SyncFrameData) {
//                return;
//            }
//            actionMonitorInfo.requestTimes += costTime;
//            actionMonitorInfo.successCount++;
//    }
//
//    public static synchronized void addActionSuccessPush(KcpSession session, FProto.Action action ) {
//        Map<FProto.Action, ActionMonitorInfo> actionMonitorInfoMap = actionReqCount.computeIfAbsent(session, (k) -> new HashMap<>());
//        ActionMonitorInfo actionMonitorInfo = actionMonitorInfoMap.computeIfAbsent(action, (k) -> new ActionMonitorInfo(session, action, 0));
//        actionMonitorInfo.requestCount ++;
//        actionMonitorInfo.requestTimes = 0;
//        actionMonitorInfo.successCount++;
//    }
//
//    public static void addActionError(KcpSession session , FProto.Action action , long costTime) {
//        Map<FProto.Action, ActionMonitorInfo> actionMonitorInfoMap = actionReqCount.get(session);
//        ActionMonitorInfo actionMonitorInfo = actionMonitorInfoMap.get(action);
//        actionMonitorInfo.requestTimes += costTime;
//        actionMonitorInfo.errorCount ++;
//    }
//
//    public static synchronized void printInfo(String name) {
////        System.err.println("机器人开始统计汇总：");
//        Map<FProto.Action, Set<ActionMonitorInfo>> actionMonitorInfos = new HashMap<>();
//        for (Map.Entry<KcpSession, Map<FProto.Action, ActionMonitorInfo>> entry : actionReqCount.entrySet()) {
//            RobotInfo robotInfo = RobotManager.get(entry.getKey());
////            System.err.println(robotInfo.toString() + " -- ");
//            entry.getValue().values().forEach(actionMonitorInfo -> {
////                System.err.println("        " +actionMonitorInfo.toString());
//                Set<ActionMonitorInfo> actionMonitorInfoSet = actionMonitorInfos.computeIfAbsent(actionMonitorInfo.action, k->new HashSet<>());
//                actionMonitorInfoSet.add(actionMonitorInfo);
//            });
//
////            System.err.println();
//        }
//        System.err.println("---------------------     "+name+"开始汇总     ------------------------");
//
//        actionMonitorInfos.forEach((k,v) -> {
//            int actionRequestCount = v.stream().mapToInt(value -> value.requestCount).sum();
//            int actionSuccessCount = v.stream().mapToInt(value -> value.successCount).sum();
//            int actionErrorCount = v.stream().mapToInt(value -> value.errorCount).sum();
//            long actionTimeCount = v.stream().mapToLong(value -> value.requestTimes).sum();
//            long averageTime = actionSuccessCount > 0 ? actionTimeCount /actionSuccessCount : (actionRequestCount > 0 ? actionTimeCount / actionRequestCount : 0);
//            System.err.println(k + " --  requestCount:" +actionRequestCount + " , requestSuccessCount:" + actionSuccessCount + " , requestErrorCount:" + actionErrorCount + " , requestTimeCount:" + actionTimeCount  + ",  averageTime:" + averageTime);
//        });
//    }
//}
